const base_url = 'https://rongyaotupay.aokxm.com'

export default function ({url, data={}, method='GET', type='application/x-www-form-urlencoded'}) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: base_url + url,
      method,
      data,
      header: {
        'userToken': wx.getStorageSync('userToken'),
        'content-type': type
      },
      success: (response) => {
        const data = response.data
        if (data.type == 'success') {
          console.log(`%c${url} Result:`, 'color: green', data.content)
          // if (data.desc) {
          //   wx.showToast({
          //     title: data.desc,
          //     icon: 'none',
          //     duration: 1500
          //   })
          // }
          resolve(data.content)
        } else {
          console.log(`%c${url} Result:`, 'color: red', data)
          if (data.desc == "请求头userToken为空" || data.desc == '请求头userToken过期') {
            setTimeout(() => {
              wx.redirectTo({
                url: '/pages/login/index',
              })
            }, 1500)
            wx.showToast({
              title: '请先登录',
              icon: 'none',
              duration: 1500
            })
          } else {
            wx.showToast({
              title: data.desc,
              icon: 'none',
              duration: 1500
            })
          }
          reject(data)
        }
      },
      fail: (error) => {
        console.log('err' + error)// for debug
        wx.showToast({
          title: data.error,
          icon: 'none'
        })
        reject(error)
      }
    })
  })
}